package com.jai.pms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.BaseDataEntity;
import com.jai.pms.domain.Project;
import com.jai.pms.mapper.ProjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.SceneMapper;
import com.jai.pms.domain.Scene;
import com.jai.pms.service.ISceneService;

/**
 * 场次管理Service业务层处理
 *
 * @author hailongzhang
 * @date 2023-09-07
 */
@Service
public class SceneServiceImpl extends BaseDataImpl implements ISceneService {
    @Autowired
    private SceneMapper sceneMapper;

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询场次管理
     *
     * @param id 场次管理主键
     * @return 场次管理
     */
    @Override
    public Scene selectSceneById(Long id) {
        return sceneMapper.selectSceneById(id);
    }

    /**
     * 查询场次管理列表
     *
     * @param scene 场次管理
     * @return 场次管理
     */
    @Override
    public List<Scene> selectSceneList(Scene scene) {
        return sceneMapper.selectSceneList(scene);
    }

    /**
     * 新增场次管理
     *
     * @param scene 场次管理
     * @return 结果
     */
    @Override
    public int insertScene(Scene scene) {
        preCheck(scene, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        scene.setCreateBy(loginUser.getUsername());
        scene.setCreateTime(DateUtils.getNowDate());
        return sceneMapper.insertScene(scene);
    }

    /**
     * 修改场次管理
     *
     * @param scene 场次管理
     * @return 结果
     */
    @Override
    public int updateScene(Scene scene) {
        preCheck(scene, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        scene.setUpdateBy(loginUser.getUsername());
        scene.setUpdateTime(DateUtils.getNowDate());
        return sceneMapper.updateScene(scene);
    }

    /**
     * 批量删除场次管理
     *
     * @param ids 需要删除的场次管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneByIds(Long[] ids) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        params.put("updateBy", SecurityUtils.getLoginUser().getUsername());
        params.put("updateTime", DateUtils.getNowDate());
        return sceneMapper.deleteSceneByIds(params);
    }

    /**
     * 删除场次管理信息
     *
     * @param id 场次管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneById(Long id) {
        return sceneMapper.deleteSceneById(id);
    }

    private Scene preCheck(Scene scene, boolean isAdd) {
        // 检查是否有重复信息，联合主键projectId和code
        Scene query = new Scene();
        query.setProjectId(scene.getProjectId());
        query.setCode(scene.getCode());
        List<Scene> queryList = sceneMapper.selectSceneList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), scene.getId())) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        }

        // 填充父类详细信息
        BaseDataEntity parent = getParent(scene.getProjectId(), "scene", scene.getParentCode());
        scene.setParentId(parent.getId());
        scene.setParentCode(parent.getCode());
        scene.setParentName(parent.getName());
        scene.setAncestors(parent.getAncestors());

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(scene.getProjectId());
        scene.setProjectCode(project.getCode());
        scene.setProjectName(project.getName());

        return scene;
    }
}
